package com.huiyujiaoyu.powertime.ui.my;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.huiyujiaoyu.base.BaseActivity;
import com.huiyujiaoyu.http.HttpApi;
import com.huiyujiaoyu.http.HttpMethods;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.IndexItemModel;
import com.huiyujiaoyu.powertime.model.SimpleListBean;
import com.huiyujiaoyu.powertime.model.UserInfoBean;
import com.huiyujiaoyu.util.ActivityUtil;
import com.huiyujiaoyu.util.DisplayUtil;
import com.huiyujiaoyu.util.GlideUtil;
import com.huiyujiaoyu.util.NumberUtil;
import com.huiyujiaoyu.util.StringUtils;
import com.huiyujiaoyu.widget.CustomLoadMoreView;
import com.huiyujiaoyu.widget.LlkjToolBar;
import com.huiyujiaoyu.widget.RoundProgressBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by win7 on 2017/6/18.
 * 我的收藏
 */

public class MyJiFenActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    LlkjToolBar mToolbar;
    @BindView(R.id.progress)
    RoundProgressBar mProgress;
    @BindView(R.id.iv_integral)
    ImageView mIvIntegral;
    @BindView(R.id.tv_integral)
    TextView mTvIntegral;
    @BindView(R.id.rv_content)
    RecyclerView mRvContent;
    private int page=1;
    private BaseQuickAdapter<IndexItemModel, BaseViewHolder> adapter;

    private List<IndexItemModel> datas;
    public static void launch(Context context) {
        ActivityUtil.startActivity(context, MyJiFenActivity.class);
    }

    @Override
    protected void initView() {
        super.initView();
        setToolbar(mToolbar);
        mToolbar.setToolBar("我的积分", true, "", R.mipmap.icon_titlebar_back, false, "", LlkjToolBar.isNull);
        mProgress.setProgress(NumberUtil.toInt(UserInfoBean.getInstance().getScore()));
        mTvIntegral.setText(UserInfoBean.getInstance().getScore());
        getData();
    }


    @Override
    protected void initData() {
        super.initData();
        datas = new ArrayList<>();
        adapter = new BaseQuickAdapter<IndexItemModel, BaseViewHolder>(R.layout.cell_integral, datas) {
            @Override
            protected void convert(BaseViewHolder helper, IndexItemModel item) {
                    helper.setText(R.id.tv_content,item.title);
                    helper.setText(R.id.tv_count,"+"+item.score);
            }
        };
        mRvContent.setLayoutManager(new LinearLayoutManager(mContext));
        mRvContent.setHasFixedSize(true);
        adapter.setLoadMoreView(new CustomLoadMoreView());
        mRvContent.setAdapter(adapter);
    }

    private void getData(){
        HttpMethods.getInstance().score_list(mContext,getComp(),this,page);
    }
    @Override
    protected void initListener() {
        super.initListener();
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        }, mRvContent);
    }

    private void loadMore() {
        page++;
        getData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_integral;
    }


    public void onViewClicked(View v) {
        switch (v.getId()) {

        }
    }

    public void doData(List<IndexItemModel> data) {
        if (page == 1) {
            datas.clear();
            // 设置加载更多能用
            datas.addAll(data);
            adapter.notifyDataSetChanged();
            adapter.setEnableLoadMore(true);
            Log.e(TAG, "page == 1");
        } else {
            if (data == null || data.size() == 0) {
                adapter.loadMoreEnd();
            } else {
                datas.addAll(data);
                adapter.notifyDataSetChanged();
                adapter.loadMoreComplete();
            }
        }
    }

    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);
        switch (httpcode) {
            case HttpApi.HTTP_SCORE_LIST:
                SimpleListBean b = (SimpleListBean) o;
                doData(b.list);
                break;
        }
    }
}
